﻿using System;
using System.Drawing;

namespace Sys.Excel
{
    [Serializable]
    public struct CellStyle : IEquatable<CellStyle>
    {
        private const int CODE = -1521134295;

        public static readonly CellStyle Body = new CellStyle(Color.Black, Color.White, Align.Left, FontBoldWeight.Normal);

        public static readonly CellStyle Header = new CellStyle(Color.Black, Color.FromArgb(226, 226, 226), Align.Center, FontBoldWeight.Bold);

        public enum Align { Left, Center, Right }

        public enum FontBoldWeight { Normal, Bold }

        public CellStyle(
            Color? fg_color = null,
            Color? bg_color = null,
            Align? align = null,
            FontBoldWeight? weight = null)
        {
            this.FgColor = fg_color;
            this.BgColor = bg_color;
            this.TextAlign = align;
            this.FontWeight = weight;
        }

        public Color? FgColor { get; set; }

        public Color? BgColor { get; set; }

        public Align? TextAlign { get; set; }

        public FontBoldWeight? FontWeight { get; set; }

        public static bool operator ==(CellStyle left, CellStyle right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(CellStyle left, CellStyle right)
        {
            return !left.Equals(right);
        }

        public override bool Equals(object obj)
        {
            if (obj != null && obj is CellStyle style)
            {
                return this.Equals(style);
            }

            return false;
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return
                    (this.FgColor?.GetHashCode() ?? 0) * CODE +
                    (this.BgColor?.GetHashCode() ?? 0) * CODE +
                    (this.TextAlign?.GetHashCode() ?? 0) * CODE +
                    (this.FontWeight?.GetHashCode() ?? 0);
            }
        }

        public bool Equals(CellStyle other)
        {
            if (this.FgColor != other.FgColor)
            {
                return false;
            }

            if (this.BgColor != other.BgColor)
            {
                return false;
            }

            if (this.TextAlign != other.TextAlign)
            {
                return false;
            }

            if (this.FontWeight != other.FontWeight)
            {
                return false;
            }

            return true;
        }
    }
}